package tw.asap.plus.ui;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.qfpay.near.uimodule.builder.CustomDialogBuilder;

import net.steamcrafted.loadtoast.LoadToast;

import butterknife.ButterKnife;
import butterknife.InjectView;
import tw.asap.plus.R;
import tw.asap.plus.app.NearApplication;
import tw.asap.plus.common.ConstValue;
import tw.asap.plus.ui.custom.AppBar;
import tw.asap.plus.utils.AppManager;
import tw.asap.plus.utils.SPUtil;
import tw.asap.plus.utils.ScreenUtil;


public class BaseActivity extends AppCompatActivity {

    protected SPUtil spUtil;
    protected String username = "", password = "";

    @InjectView(R.id.appbar)
    AppBar appbar;
    @InjectView(R.id.layout_content)
    FrameLayout layoutContent;

    private View rootLayoutView;
    private FragmentManager supportFragmentManager;

    public LoadToast loadToast;

    /**
     * Called when the activity is first created.
     */

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        loadToast = new LoadToast(this);
        loadToast.setBackgroundColor(getResources().getColor(R.color.palette_white))
                .setProgressColor(getResources().getColor(R.color.palette_red))
                .setTextColor(getResources().getColor(R.color.palette_red));
        loadToast.setTranslationY(ScreenUtil.dip2px(NearApplication.getNearApplicationContext(), 50));
        rootLayoutView = LayoutInflater.from(this).inflate(R.layout.activity_base_content, null);
        supportFragmentManager = getSupportFragmentManager();
        super.setContentView(rootLayoutView);
        ButterKnife.inject(this,rootLayoutView);
        AppManager.getAppManager().addActivity(this);
        spUtil = SPUtil.getInstance(BaseActivity.this);
        if (TextUtils.isEmpty(ConstValue.cookie)) {
            ConstValue.cookie = spUtil.getString(ConstValue.Sp.COOKIE, "");
        }
        appbar.setVisibility(View.GONE);
    }



    /**
     * 切換Fragment
     */
    public void changeFragment(Fragment f) {
        changeFragment(f, false);
    }


    /**
     * 初始化Fragment(FragmentActivity中呼叫)
     */
    public void initFragment(Fragment f) {
        changeFragment(f, true);
    }


    private void changeFragment(Fragment f, boolean init) {
        if(f.getArguments() == null){
            Bundle bundle = new Bundle();
            f.setArguments(bundle);
        }
        FragmentTransaction ft = supportFragmentManager.beginTransaction();
//        ft.setCustomAnimations(R.anim.fragments_transition_in, R.anim.fragments_transition_out);
        ft.replace(R.id.layout_content, f,f.getClass().getName());
        if (!init)
            ft.addToBackStack(null);
        ft.commit();
    }

    public void setHasAppBar(boolean hasAppBar) {
        if (!hasAppBar) {
            appbar.setVisibility(View.GONE);
        } else {
            appbar.setVisibility(View.VISIBLE);
        }
    }

    public AppBar getAppBar() {
        return appbar;
    }


    @Override
    public void setContentView(int layoutResID) {
        LayoutInflater inflater = LayoutInflater.from(this);
        View v = inflater.inflate(layoutResID, layoutContent);
        super.setContentView(rootLayoutView);
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }



    @Override
    protected void onDestroy() {
        super.onDestroy();
        AppManager.getAppManager().finishActivity(this);
    }

    protected void showNormalDialog(String title, String message, final DialogInterface.OnClickListener okListener, final DialogInterface.OnClickListener cancelListener) {
        CustomDialogBuilder.buildDoubleBtnDialog(this)
                .setMsg(message)
                .setConfirmBtnText(getString(R.string.text_ok))
                .setTouchOutsideCanDismiss(true)
                .setOnConfrimClickListener(new CustomDialogBuilder.OnConfrimClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog) {
                        if (null != dialog) dialog.dismiss();
                        if (null == okListener) return;
                        okListener.onClick(dialog, 1);


                    }
                })
                .setCancelBtnText(getString(R.string.text_cancel))
                .setCancelClickListener(new CustomDialogBuilder.OnCancelCLickListener() {
                    @Override
                    public void onClick(DialogInterface dialog) {
                        if (null != dialog) dialog.dismiss();
                        if (null == cancelListener) return;
                        cancelListener.onClick(dialog, 0);

                    }
                }).getDialog().show();
    }

    /**
     * 是否含有用户信息
     */
    protected boolean hasLoginForm() {
        username = spUtil.getString(ConstValue.Sp.USERNAME, "");
//        password = spUtil.getString(ConstValue.Sp.PASSWORD, "");
        String cookie = spUtil.getString(ConstValue.Sp.COOKIE, "");
        return (!TextUtils.isEmpty(username)/* && !TextUtils.isEmpty(password)*/ && !TextUtils.isEmpty(cookie));
    }


}